home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / schema.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  19.5 KB  |  597 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. """The schema module is responsible for defining what data in the database
  5. gets stored on disk.  
  6.  
  7. The goals of this modules are:
  8.  
  9. * Clearly defining which data from DDBObjects gets stored and which doesn't.
  10. * Validating that all data we write can be read back in
  11. * Making upgrades of the database schema as easy as possible
  12.  
  13. Module-level variables:
  14.     objectSchemas -- Schemas to use with the current database.
  15.     VERSION -- Current schema version.  If you change the schema you must bump
  16.     this number and add a function in the dbupgrade module.
  17.  
  18. Go to the bottom of this file for the current database schema.
  19. """
  20. import cPickle
  21. import datetime
  22. import time
  23. import logging
  24. from types import NoneType
  25. from fasttypes import LinkedList
  26. from platformutils import FilenameType
  27.  
  28. class ValidationError(Exception):
  29.     '''Error thrown when we try to save invalid data.'''
  30.     pass
  31.  
  32.  
  33. class ValidationWarning(Warning):
  34.     '''Warning issued when we try to restore invalid data.'''
  35.     pass
  36.  
  37.  
  38. class SchemaItem(object):
  39.     '''SchemaItem represents a single attribute that gets stored on disk.
  40.  
  41.     SchemaItem is an abstract class.  Subclasses of SchemaItem such as
  42.     SchemaAttr, SchemaObject, SchemaList are used in actual object schemas.
  43.  
  44.     Member variables:
  45.         noneOk -- specifies if None is a valid value for this attribute
  46.     '''
  47.     
  48.     def __init__(self, noneOk = False):
  49.         self.noneOk = noneOk
  50.  
  51.     
  52.     def validate(self, data):
  53.         '''Validate that data is a valid value for this SchemaItem.
  54.  
  55.         validate is "dumb" when it comes to container types like SchemaList,
  56.         etc.  It only checks that the container is the right type, not its
  57.         children.  This isn\'t a problem because saveObject() calls
  58.         validate() recursively on all the data it saves, therefore validate
  59.         doesn\'t have to recirsively validate things.
  60.         '''
  61.         if data is None:
  62.             if not self.noneOk:
  63.                 raise ValidationError('None value is not allowed')
  64.             
  65.         
  66.         return True
  67.  
  68.     
  69.     def validateType(self, data, correctType):
  70.         '''Helper function that many subclasses use'''
  71.         if data is not None and not isinstance(data, correctType):
  72.             raise ValidationError('%r (type: %s) is not a %s' % (data, type(data), correctType))
  73.         
  74.  
  75.     
  76.     def validateTypes(self, data, possibleTypes):
  77.         if data is None:
  78.             return None
  79.         
  80.         for t in possibleTypes:
  81.             if isinstance(data, t):
  82.                 return None
  83.                 continue
  84.         
  85.         raise ValidationError('%r (type: %s) is not any of: %s' % (data, type(data), possibleTypes))
  86.  
  87.  
  88.  
  89. class SchemaSimpleItem(SchemaItem):
  90.     '''Base class for SchemaItems for simple python types.'''
  91.     pass
  92.  
  93.  
  94. class SchemaBool(SchemaSimpleItem):
  95.     
  96.     def validate(self, data):
  97.         super(SchemaSimpleItem, self).validate(data)
  98.         self.validateType(data, bool)
  99.  
  100.  
  101.  
  102. class SchemaFloat(SchemaSimpleItem):
  103.     
  104.     def validate(self, data):
  105.         super(SchemaSimpleItem, self).validate(data)
  106.         self.validateType(data, float)
  107.  
  108.  
  109.  
  110. class SchemaString(SchemaSimpleItem):
  111.     
  112.     def validate(self, data):
  113.         super(SchemaSimpleItem, self).validate(data)
  114.         self.validateType(data, unicode)
  115.  
  116.  
  117.  
  118. class SchemaBinary(SchemaSimpleItem):
  119.     
  120.     def validate(self, data):
  121.         super(SchemaSimpleItem, self).validate(data)
  122.         self.validateType(data, str)
  123.  
  124.  
  125.  
  126. class SchemaFilename(SchemaSimpleItem):
  127.     
  128.     def validate(self, data):
  129.         super(SchemaSimpleItem, self).validate(data)
  130.         self.validateType(data, FilenameType)
  131.  
  132.  
  133.  
  134. class SchemaURL(SchemaSimpleItem):
  135.     
  136.     def validate(self, data):
  137.         super(SchemaSimpleItem, self).validate(data)
  138.         self.validateType(data, unicode)
  139.         if data:
  140.             
  141.             try:
  142.                 data.encode('ascii')
  143.             except UnicodeEncodeError:
  144.                 ValidationError(u'URL (%s) is not ASCII' % data)
  145.             except:
  146.                 None<EXCEPTION MATCH>UnicodeEncodeError
  147.             
  148.  
  149.         None<EXCEPTION MATCH>UnicodeEncodeError
  150.  
  151.  
  152.  
  153. class SchemaInt(SchemaSimpleItem):
  154.     
  155.     def validate(self, data):
  156.         super(SchemaSimpleItem, self).validate(data)
  157.         self.validateTypes(data, [
  158.             int,
  159.             long])
  160.  
  161.  
  162.  
  163. class SchemaDateTime(SchemaSimpleItem):
  164.     
  165.     def validate(self, data):
  166.         super(SchemaSimpleItem, self).validate(data)
  167.         self.validateType(data, datetime.datetime)
  168.  
  169.  
  170.  
  171. class SchemaTimeDelta(SchemaSimpleItem):
  172.     
  173.     def validate(self, data):
  174.         super(SchemaSimpleItem, self).validate(data)
  175.         self.validateType(data, datetime.timedelta)
  176.  
  177.  
  178.  
  179. class SchemaList(SchemaItem):
  180.     
  181.     def __init__(self, childSchema, noneOk = False):
  182.         super(SchemaList, self).__init__(noneOk)
  183.         self.childSchema = childSchema
  184.  
  185.     
  186.     def validate(self, data):
  187.         super(SchemaList, self).validate(data)
  188.         self.validateType(data, list)
  189.  
  190.  
  191.  
  192. class SchemaDict(SchemaItem):
  193.     type = dict
  194.     
  195.     def __init__(self, keySchema, valueSchema, noneOk = False):
  196.         super(SchemaDict, self).__init__(noneOk)
  197.         self.keySchema = keySchema
  198.         self.valueSchema = valueSchema
  199.  
  200.     
  201.     def validate(self, data):
  202.         super(SchemaDict, self).validate(data)
  203.         self.validateType(data, dict)
  204.  
  205.  
  206.  
  207. class SchemaSimpleContainer(SchemaSimpleItem):
  208.     '''Allows nested dicts, lists and tuples, however the only thing they can
  209.     store are simple objects.  This currently includes bools, ints, longs,
  210.     floats, strings, unicode, None, datetime and struct_time objects.
  211.     '''
  212.     
  213.     def validate(self, data):
  214.         super(SchemaSimpleContainer, self).validate(data)
  215.         self.validateTypes(data, (dict, list, tuple))
  216.         self.memory = set()
  217.         toValidate = LinkedList()
  218.         while data:
  219.             if id(data) in self.memory:
  220.                 return None
  221.             else:
  222.                 self.memory.add(id(data))
  223.             if isinstance(data, list) or isinstance(data, tuple):
  224.                 for item in data:
  225.                     toValidate.append(item)
  226.                 
  227.             elif isinstance(data, dict):
  228.                 for key, value in data.items():
  229.                     self.validateTypes(key, [
  230.                         bool,
  231.                         int,
  232.                         long,
  233.                         float,
  234.                         unicode,
  235.                         str,
  236.                         NoneType,
  237.                         datetime.datetime,
  238.                         time.struct_time])
  239.                     toValidate.append(value)
  240.                 
  241.             else:
  242.                 self.validateTypes(data, [
  243.                     bool,
  244.                     int,
  245.                     long,
  246.                     float,
  247.                     unicode,
  248.                     NoneType,
  249.                     datetime.datetime,
  250.                     time.struct_time])
  251.             
  252.             try:
  253.                 data = toValidate.pop()
  254.             continue
  255.             data = None
  256.             continue
  257.  
  258.  
  259.  
  260.  
  261. class SchemaStatusContainer(SchemaSimpleContainer):
  262.     '''Allows nested dicts, lists and tuples, however the only thing they can
  263.     store are simple objects.  This currently includes bools, ints, longs,
  264.     floats, strings, unicode, None, datetime and struct_time objects.
  265.     '''
  266.     
  267.     def validate(self, data):
  268.         FilenameType = FilenameType
  269.         import platformutils
  270.         if FilenameType == unicode:
  271.             binaryFields = [
  272.                 'metainfo',
  273.                 'fastResumeData']
  274.         else:
  275.             binaryFields = [
  276.                 'channelName',
  277.                 'shortFilename',
  278.                 'filename',
  279.                 'metainfo',
  280.                 'fastResumeData']
  281.         self.validateType(data, dict)
  282.         for key, value in data.items():
  283.             self.validateTypes(key, [
  284.                 bool,
  285.                 int,
  286.                 long,
  287.                 float,
  288.                 unicode,
  289.                 str,
  290.                 NoneType,
  291.                 datetime.datetime,
  292.                 time.struct_time])
  293.             if key not in binaryFields:
  294.                 self.validateTypes(value, [
  295.                     bool,
  296.                     int,
  297.                     long,
  298.                     float,
  299.                     unicode,
  300.                     NoneType,
  301.                     datetime.datetime,
  302.                     time.struct_time])
  303.                 continue
  304.             self.validateType(value, str)
  305.         
  306.  
  307.  
  308.  
  309. class SchemaObject(SchemaItem):
  310.     
  311.     def __init__(self, klass, noneOk = False):
  312.         super(SchemaObject, self).__init__(noneOk)
  313.         self.klass = klass
  314.  
  315.     
  316.     def validate(self, data):
  317.         super(SchemaObject, self).validate(data)
  318.         self.validateType(data, self.klass)
  319.  
  320.  
  321.  
  322. class ObjectSchema(object):
  323.     """The schema to save/restore an object with.  Object schema isn't a
  324.     SchemaItem, it's the schema for an entire object.
  325.  
  326.     Member variables:
  327.  
  328.     klass -- the python class that this schema is for
  329.     classString -- a human readable string that represents objectClass
  330.     fields -- list of  (name, SchemaItem) pairs.  One item for each attribute
  331.         that shoud be stored to disk.
  332.     """
  333.     pass
  334.  
  335. from database import DDBObject
  336. from downloader import RemoteDownloader, HTTPAuthPassword
  337. from feed import Feed, FeedImpl, RSSFeedImpl, ScraperFeedImpl
  338. from feed import SearchFeedImpl, DirectoryWatchFeedImpl, DirectoryFeedImpl, SearchDownloadsFeedImpl
  339. from feed import ManualFeedImpl, SingleFeedImpl
  340. from folder import ChannelFolder, PlaylistFolder
  341. from guide import ChannelGuide
  342. from item import Item, FileItem
  343. from iconcache import IconCache
  344. from playlist import SavedPlaylist
  345. from tabs import TabOrder
  346. from theme import ThemeHistory
  347.  
  348. class DDBObjectSchema(ObjectSchema):
  349.     klass = DDBObject
  350.     classString = 'ddb-object'
  351.     fields = [
  352.         ('id', SchemaInt())]
  353.  
  354.  
  355. class IconCacheSchema(ObjectSchema):
  356.     klass = IconCache
  357.     classString = 'icon-cache'
  358.     fields = [
  359.         ('etag', SchemaString(noneOk = True)),
  360.         ('modified', SchemaString(noneOk = True)),
  361.         ('filename', SchemaFilename(noneOk = True)),
  362.         ('resized_filenames', SchemaDict(SchemaString(), SchemaFilename())),
  363.         ('url', SchemaURL(noneOk = True))]
  364.  
  365.  
  366. class ItemSchema(DDBObjectSchema):
  367.     klass = Item
  368.     classString = 'item'
  369.     fields = DDBObjectSchema.fields + [
  370.         ('feed_id', SchemaInt(noneOk = True)),
  371.         ('parent_id', SchemaInt(noneOk = True)),
  372.         ('seen', SchemaBool()),
  373.         ('autoDownloaded', SchemaBool()),
  374.         ('pendingManualDL', SchemaBool()),
  375.         ('pendingReason', SchemaString()),
  376.         ('entry', SchemaSimpleContainer()),
  377.         ('expired', SchemaBool()),
  378.         ('keep', SchemaBool()),
  379.         ('creationTime', SchemaDateTime()),
  380.         ('linkNumber', SchemaInt(noneOk = True)),
  381.         ('iconCache', SchemaObject(IconCache, noneOk = True)),
  382.         ('downloadedTime', SchemaDateTime(noneOk = True)),
  383.         ('watchedTime', SchemaDateTime(noneOk = True)),
  384.         ('isContainerItem', SchemaBool(noneOk = True)),
  385.         ('videoFilename', SchemaFilename()),
  386.         ('isVideo', SchemaBool()),
  387.         ('releaseDateObj', SchemaDateTime()),
  388.         ('eligibleForAutoDownload', SchemaBool()),
  389.         ('duration', SchemaInt(noneOk = True)),
  390.         ('screenshot', SchemaFilename(noneOk = True)),
  391.         ('resized_screenshots', SchemaDict(SchemaString(), SchemaFilename())),
  392.         ('resumeTime', SchemaInt())]
  393.  
  394.  
  395. class FileItemSchema(ItemSchema):
  396.     klass = FileItem
  397.     classString = 'file-item'
  398.     fields = ItemSchema.fields + [
  399.         ('filename', SchemaFilename()),
  400.         ('deleted', SchemaBool()),
  401.         ('shortFilename', SchemaFilename(noneOk = True)),
  402.         ('offsetPath', SchemaFilename(noneOk = True))]
  403.  
  404.  
  405. class FeedSchema(DDBObjectSchema):
  406.     klass = Feed
  407.     classString = 'feed'
  408.     fields = DDBObjectSchema.fields + [
  409.         ('origURL', SchemaURL()),
  410.         ('errorState', SchemaBool()),
  411.         ('loading', SchemaBool()),
  412.         ('actualFeed', SchemaObject(FeedImpl)),
  413.         ('iconCache', SchemaObject(IconCache, noneOk = True)),
  414.         ('folder_id', SchemaInt(noneOk = True)),
  415.         ('searchTerm', SchemaString(noneOk = True)),
  416.         ('userTitle', SchemaString(noneOk = True)),
  417.         ('autoDownloadable', SchemaBool()),
  418.         ('getEverything', SchemaBool()),
  419.         ('maxNew', SchemaInt()),
  420.         ('fallBehind', SchemaInt()),
  421.         ('expire', SchemaString()),
  422.         ('expireTime', SchemaTimeDelta(noneOk = True))]
  423.  
  424.  
  425. class FeedImplSchema(ObjectSchema):
  426.     klass = FeedImpl
  427.     classString = 'field-impl'
  428.     fields = [
  429.         ('url', SchemaURL()),
  430.         ('ufeed', SchemaObject(Feed)),
  431.         ('title', SchemaString()),
  432.         ('created', SchemaDateTime()),
  433.         ('visible', SchemaBool()),
  434.         ('lastViewed', SchemaDateTime()),
  435.         ('thumbURL', SchemaURL(noneOk = True)),
  436.         ('updateFreq', SchemaInt()),
  437.         ('initialUpdate', SchemaBool())]
  438.  
  439.  
  440. class RSSFeedImplSchema(FeedImplSchema):
  441.     klass = RSSFeedImpl
  442.     classString = 'rss-feed-impl'
  443.     fields = FeedImplSchema.fields + [
  444.         ('initialHTML', SchemaBinary(noneOk = True)),
  445.         ('etag', SchemaString(noneOk = True)),
  446.         ('modified', SchemaString(noneOk = True))]
  447.  
  448.  
  449. class ScraperFeedImplSchema(FeedImplSchema):
  450.     klass = ScraperFeedImpl
  451.     classString = 'scraper-feed-impl'
  452.     fields = FeedImplSchema.fields + [
  453.         ('initialHTML', SchemaBinary(noneOk = True)),
  454.         ('initialCharset', SchemaString(noneOk = True)),
  455.         ('linkHistory', SchemaSimpleContainer())]
  456.  
  457.  
  458. class SearchFeedImplSchema(FeedImplSchema):
  459.     klass = SearchFeedImpl
  460.     classString = 'search-feed-impl'
  461.     fields = FeedImplSchema.fields + [
  462.         ('searching', SchemaBool()),
  463.         ('lastEngine', SchemaString()),
  464.         ('lastQuery', SchemaString())]
  465.  
  466.  
  467. class DirectoryWatchFeedImplSchema(FeedImplSchema):
  468.     klass = DirectoryWatchFeedImpl
  469.     classString = 'directory-watch-feed-impl'
  470.     fields = FeedImplSchema.fields + [
  471.         ('firstUpdate', SchemaBool()),
  472.         ('dir', SchemaFilename(noneOk = True))]
  473.  
  474.  
  475. class DirectoryFeedImplSchema(FeedImplSchema):
  476.     klass = DirectoryFeedImpl
  477.     classString = 'directory-feed-impl'
  478.  
  479.  
  480. class SearchDownloadsFeedImplSchema(FeedImplSchema):
  481.     klass = SearchDownloadsFeedImpl
  482.     classString = 'search-downloads-feed-impl'
  483.  
  484.  
  485. class ManualFeedImplSchema(FeedImplSchema):
  486.     klass = ManualFeedImpl
  487.     classString = 'manual-feed-impl'
  488.  
  489.  
  490. class SingleFeedImplSchema(FeedImplSchema):
  491.     klass = SingleFeedImpl
  492.     classString = 'single-feed-impl'
  493.  
  494.  
  495. class RemoteDownloaderSchema(DDBObjectSchema):
  496.     klass = RemoteDownloader
  497.     classString = 'remote-downloader'
  498.     fields = DDBObjectSchema.fields + [
  499.         ('url', SchemaURL()),
  500.         ('origURL', SchemaURL()),
  501.         ('dlid', SchemaString()),
  502.         ('contentType', SchemaString(noneOk = True)),
  503.         ('channelName', SchemaFilename(noneOk = True)),
  504.         ('status', SchemaStatusContainer()),
  505.         ('manualUpload', SchemaBool())]
  506.  
  507.  
  508. class HTTPAuthPasswordSchema(DDBObjectSchema):
  509.     klass = HTTPAuthPassword
  510.     classString = 'http-auth-password'
  511.     fields = DDBObjectSchema.fields + [
  512.         ('username', SchemaString()),
  513.         ('password', SchemaString()),
  514.         ('host', SchemaString()),
  515.         ('realm', SchemaString()),
  516.         ('path', SchemaString()),
  517.         ('authScheme', SchemaString())]
  518.  
  519.  
  520. class ChannelFolderSchema(DDBObjectSchema):
  521.     klass = ChannelFolder
  522.     classString = 'channel-folder'
  523.     fields = DDBObjectSchema.fields + [
  524.         ('expanded', SchemaBool()),
  525.         ('title', SchemaString())]
  526.  
  527.  
  528. class PlaylistFolderSchema(DDBObjectSchema):
  529.     klass = PlaylistFolder
  530.     classString = 'playlist-folder'
  531.     fields = DDBObjectSchema.fields + [
  532.         ('expanded', SchemaBool()),
  533.         ('title', SchemaString()),
  534.         ('item_ids', SchemaList(SchemaInt()))]
  535.  
  536.  
  537. class PlaylistSchema(DDBObjectSchema):
  538.     klass = SavedPlaylist
  539.     classString = 'playlist'
  540.     fields = DDBObjectSchema.fields + [
  541.         ('title', SchemaString()),
  542.         ('item_ids', SchemaList(SchemaInt())),
  543.         ('folder_id', SchemaInt(noneOk = True))]
  544.  
  545.  
  546. class TabOrderSchema(DDBObjectSchema):
  547.     klass = TabOrder
  548.     classString = 'taborder-order'
  549.     fields = DDBObjectSchema.fields + [
  550.         ('type', SchemaString()),
  551.         ('tab_ids', SchemaList(SchemaInt()))]
  552.  
  553.  
  554. class ChannelGuideSchema(DDBObjectSchema):
  555.     klass = ChannelGuide
  556.     classString = 'channel-guide'
  557.     fields = DDBObjectSchema.fields + [
  558.         ('url', SchemaURL(noneOk = True)),
  559.         ('updated_url', SchemaURL(noneOk = True)),
  560.         ('favicon', SchemaURL(noneOk = True)),
  561.         ('title', SchemaString(noneOk = True)),
  562.         ('iconCache', SchemaObject(IconCache, noneOk = True)),
  563.         ('firstTime', SchemaBool())]
  564.  
  565.  
  566. class ThemeHistorySchema(DDBObjectSchema):
  567.     klass = ThemeHistory
  568.     classString = 'theme-history'
  569.     fields = DDBObjectSchema.fields + [
  570.         ('lastTheme', SchemaString(noneOk = True)),
  571.         ('pastThemes', SchemaList(SchemaString(noneOk = False), noneOk = False))]
  572.  
  573. VERSION = 57
  574. objectSchemas = [
  575.     DDBObjectSchema,
  576.     IconCacheSchema,
  577.     ItemSchema,
  578.     FileItemSchema,
  579.     FeedSchema,
  580.     FeedImplSchema,
  581.     RSSFeedImplSchema,
  582.     ScraperFeedImplSchema,
  583.     SearchFeedImplSchema,
  584.     DirectoryFeedImplSchema,
  585.     DirectoryWatchFeedImplSchema,
  586.     SearchDownloadsFeedImplSchema,
  587.     RemoteDownloaderSchema,
  588.     HTTPAuthPasswordSchema,
  589.     ChannelGuideSchema,
  590.     ManualFeedImplSchema,
  591.     SingleFeedImplSchema,
  592.     PlaylistSchema,
  593.     ChannelFolderSchema,
  594.     PlaylistFolderSchema,
  595.     TabOrderSchema,
  596.     ThemeHistorySchema]
  597.